package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    // 应用ID,您的APPID，收款账号既是您的APPID对应支付宝账号
    public String app_id;

    // 商户私钥，您的PKCS8格式RSA2私钥
    public String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCQaSz0592SU2ro5Otc9SqGLTdU6klTYNtoE60JxbwfB8LJSmldIKXdHInjFq0Tv78IG6+xKFr4dePsUJS2y4uu+xnKioGnPN7s7DPpDtFDTF07JIlL2KHwoWgdHeoXUWIvJwmRR/pG+txiMyr3bHfVhcEVbUa8oBjcoPwerVWWoJQwLvG525S6sMNSF/6YC8hEL8c2jtm6W51i2ObYTwjcUwa+RB+u2/RRY4iqcV8jq13Rbvl/wlJXZNgKLUqGs7LkrFJX6KifkFKA5gFxeHg7vkor3bvYVKq+YtXmtM5mJxSSSaKttWI+UNgTrWe1MgkEAhbllqDf46YfS25JXDiZAgMBAAECggEBAIV2vtblR/EJaWAu2ZuC9jclqOKw2O3XMU/iB2VluzROfuZ8AB7SophU3NbkyTbcWthKhjjL3C+O/QQVgSvjuIgSh8aZ9bOzXTBIKL23NQ3mkK+tV7qiTyBOfCOU1xmP3JdJk90SUauyA2Y6dfZP5/yN7mI4KU0BBZk6q2hKuDRJdCWmlcNy6x2apK527Y5MyxLgSn8Tdod4qFbF+TeJakP1a+gk0/bCwItevaNEo9On9fxXafKlXE6sWNgQITode7vn5z6RW3bA+5mjlTrUgUYTfNsihu90PEL52fPxvliVlxSRf/ggH3UyMFwypR9lPRj/2vuqC51xOMH/+xjhmFECgYEA2bRuYopholQmarcU7HHJ/37+DdtjBh7JgJWeuBRXOv0zPjY7TceLRSxbirvmBVIBPKkUGsOB7VfLpTjToJxWVnurcvdCVoU/GGruvhT0WvcGX0vRqVJ6ajjLJs35u1Un09u07I3wPJRRWhyNXoN0ydycwrRxZ0tv7ZQseDGGDr0CgYEAqdA1fGxqpgLjE+xNtUPasSqM6CSTmoT91hW5fMk9dp7gjd9ZLfXX2EXlW/gUDhtgiamzDnhbzdsKOUDNGVcDtibO5H+H4ou9cnUVmiPPIayfHtJODrgRYFjdxye6W+Sufhy0lgdsseaZq9j1wYuJJdGYqkBh0xMfm1Uj3jJTbQ0CgYEAgYhbzYHsybssCH7akjy6Ja0X2ILajn+vATUE9R4VMOIKpU97RbIe6Y+VAB0ApdW/Kqqnba06F1APuUTwljWvIVIAFy2LaRNrEUo+txactasZPYZNP2P1tlIIfhQ8LPxj8ChfpAhoEoNlTCqE6uFokdsT4r2eZAl8lW1+cB13WDkCgYAThNAxDhEwJdLOHnJMLJF9ncN2h4XD1BeNPRAGDOeYs+hhxlNs2f5GaLpVWO2NNY5iJHRlbGY5Aj1pmU455GuTy2AqmZIX8zICbvzdq9IKL9pWY5v/Sx8MMQrE8ocUOvPnoQsM16uhwzzUqOqOpahNBYuDvkTAS0ltxFqMFsLKHQKBgCYXXY6pPZFJFXV6viO/ujAvovxKaLPkyXJm6AQOGOHVOEe6RFJIOuMSOSkNDu9Jg5IAWU5Mmvid0AAXsDRTW7fFB5rPZbRRGD/vwGNO8JQDVNM7Son8M3MuMoTtCRzQOcaCZZzw8MPIGd9cel/SJmyqlyFqwCiQ+Y5Mo3e5Olf3";

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    public String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnTvBM452UNwu1FXtZWyRCQzEWhND9hhoaUB3csPhyRNE648CJ7hkr2RYPyhLcWyGM1bGhEMjYUSZmTBehjy0Jy2T/u9Qcp5nMJGrNpvvhdBM2jc5YMFVceuR+VVtVxOgVZ+hohVNEckTeqUIRsJyHqOZNOsV0xsWPPeRXynm6ueU3mAhJUTA//gq8MUGX0SVNAUvZJnJvXIX7l2/e0rF9K7AH8yGOfH6gsY6/flTvzE8VdDg+UnB9Flr9+F8U+ZiDSGYfF4whjw6hWHoK5i6Oj6XTsjtkfV69GpL1rxGFShGmtmE0l8m8hfUSFBdeeERHE23hgAniztkEwcHCmPbewIDAQAB";

    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    public String notify_url;

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    public String return_url;

    // 签名方式
    private  String sign_type;

    // 字符编码格式
    private  String charset;

    //订单超时时间
    private String timeout = "1m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    public String gatewayUrl;

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}